﻿using System.Collections.Generic;
using JStore.Core.Commands;
using JStore.Core.Dtos;
using JStore.Core.Entities;
using JStore.Core.Enums;
using JStore.Data.Daos;

namespace JStore.Tasks
{
    /// <summary>
    ///
    /// </summary>
    public class CardTypeTask
    {
        private readonly CardTypeDao _cardTypeDao = new CardTypeDao();

        public List<CardType> GetAll()
        {
            return _cardTypeDao.GetAll();
        }

        public CardType GetById(int id)
        {
            return _cardTypeDao.GetById(id);
        }

        public bool Save(SaveCardTypeCmd cmd, out string errMsg)
        {
            errMsg = string.Empty;

            if (cmd.BalanceRate <= 0 || cmd.BalanceRate >= 1)
            {
                errMsg = "结算比率不正确";
                return false;
            }

            if (string.IsNullOrEmpty(cmd.CardName))
            {
                errMsg = "消费名称不能为空";
                return false;
            }

            if (cmd.Price <= 0)
            {
                errMsg = "消费金额必须大于0";
                return false;
            }

            if (cmd.Id == null)
            {
                var info = new CardType
                               {
                                   BalanceRate = cmd.BalanceRate,
                                   CardClass = (CardClass)cmd.CardClass,
                                   CardName = cmd.CardName,
                                   Price = cmd.Price,
                                   SaleRate = cmd.SaleRate,
                                   UserTypeId = cmd.UserTypeId,
                               };
                _cardTypeDao.Add(info);
                return true;
            }
            else
            {
                var info = _cardTypeDao.GetById((int)cmd.Id);

                if (info == null)
                {
                    errMsg = "消费种类不存在";
                    return false;
                }

                info.BalanceRate = cmd.BalanceRate;
                info.CardClass = (CardClass)cmd.CardClass;
                info.CardName = cmd.CardName;
                info.Price = cmd.Price;
                info.SaleRate = cmd.SaleRate;
                info.UserTypeId = cmd.UserTypeId;
                _cardTypeDao.Update(info);

                return true;
            }
        }

        public void DeleteById(int id)
        {
            _cardTypeDao.DeleteById(id);
        }

        public List<CardType> GetListByCardClass(int cardClass)
        {
            return _cardTypeDao.GetListByCardClass(cardClass);
        }

        public List<CardType> GetUpdateUserTypeList(int userTypeId, CardClass cardClass)
        {
            return _cardTypeDao.GetUpdateUserTypeList(userTypeId, cardClass);
        }

        public CardType Get(CardClass cardClass, int userTypeId)
        {
            return _cardTypeDao.Get(userTypeId, cardClass);
        }
    }
}

